package com.asnike.selector{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class CampPlaceItemList extends Sprite{
		
		private var _itemClickCallback:Function;
		private var _currentState:String
		
		public function CampPlaceItemList()
		{
			super();
			initState();
		}
		private function initState():void{
			setStateShort();
		}
		private function setStateShort():void{
			_currentState = 'shortInfo';
		}
		
		public function setData( $data:XML ):void{
			removeList();
			createList( $data );
		}
		private function removeList():void{
			while( numChildren > 0 ){
				removeChildAt( 0 );
			}
		}
		private function createList( $data:XML ):void{
			var i:int, j:int, campPlaceItem:CampPlaceItem;
			
			for( i = 0, j = $data.place.length() ; i < j ; ++i ){
				campPlaceItem = new CampPlaceItem;
				campPlaceItem.setPlaceName( $data.place[i].name.toString() );
				campPlaceItem.setDescription( $data.place[i].description.toString() );
				campPlaceItem.setPoint( $data.place[i].star_point.toString() );
				campPlaceItem.setImages( getImageURLs( $data.place[i].images ) );
				campPlaceItem.setThumbnails( getThumbnailURLs( $data.place[i].thumbnails ) );
				campPlaceItem.setDetailImage( $data.place[i].detail.@url.toString() );
				campPlaceItem.addEventListener( MouseEvent.CLICK, click );
//				campPlaceItem.doubleClickEnabled = true;
//				campPlaceItem.addEventListener( MouseEvent.DOUBLE_CLICK, doubleClick );
				campPlaceItem.name = 'item' + i;
				campPlaceItem.setClickBtnUpCallback( btnUpClick );
				campPlaceItem.x = 0;
				campPlaceItem.y = i*128;
				addChild( campPlaceItem );
			}
		}
		private function getImageURLs( $xmllist:XMLList ):Array{
			var i:int, j:int, result:Array;
			
			result = [];
			for( i = 0, j = $xmllist.image.length() ; i < j ; ++i ){
				result[result.length] = $xmllist.image[i].@url.toString();
			}
			
			return result;
		}
		private function getThumbnailURLs( $xmllist:XMLList ):Array{
			var i:int, j:int, result:Array;
			
			result = [];
			for( i = 0, j = $xmllist.thumbnail.length() ; i < j ; ++i ){
				result[result.length] = $xmllist.thumbnail[i].@url.toString();
			}
			
			return result;
		}
		private function click( $e:MouseEvent ):void{
			showCurrentItemDetailInfo( $e.currentTarget.name.substr( -1 ) );
		}
		internal function showCurrentItemDetailInfo( $index:int ):void{
			var currentItem:CampPlaceItem;
			
			hideAllItems();
			currentItem = getChildByName( 'item' + $index ) as CampPlaceItem;
			currentItem.showDetailInfo();
			currentItem.y = 0;
			setChildIndex( currentItem, numChildren - 1 );
			setStateDetail();
			
			if( _itemClickCallback is Function ){
				_itemClickCallback( $index, _currentState );
			}
		}
		private function hideAllItems():void{
			var i:int, j:int;
			
			for( i = 0, j = 5 ; i < j ; ++i ){
				CampPlaceItem( getChildByName( 'item' + i ) ).hide();
			}
		}
		private function setStateDetail():void{
			_currentState = 'detailInfo';
		}
		private function hideDetailInfoAllItems():void{
			var i:int, j:int;
			
			for( i = 0, j = 5 ; i < j ; ++i ){
				CampPlaceItem( getChildByName( 'item' + i ) ).hideDetailInfo();
				CampPlaceItem( getChildByName( 'item' + i ) ).y = i*128;
			}
		}
		private function btnUpClick( $index:int ):void{
			hideDetailInfoAllItems();
			setStateShort();
			if( _itemClickCallback is Function ){
				_itemClickCallback( $index, _currentState );
			}
		}
		
		internal function setItemClickCallback( $function:Function ):void{
			_itemClickCallback = $function;
		}
	}
}